import type { TreeNode, UserInfo } from '@vben/types';

import { requestClient } from '#/api/request';

export namespace UserApi {
  export interface PageFetchParams {
    /** 页码 */
    page: number;
    /** 每页数量 */
    pageSize: number;
    /** 用户名 */
    username?: string;
    /** 部门ID */
    deptId?: number;
    /** 真实姓名 */
    realName?: string;
    /** 邮箱 */
    email?: string;
    /** 手机号 */
    phone?: string;
  }

  export interface user {
    name: string;
    desc: string;
  }
}

/**
 * 获取用户信息
 */
export async function getUsersApi(params: UserApi.PageFetchParams) {
  // 确保 deptId 作为数字传递
  const processedParams = {
    ...params,
    ...(params.deptId !== undefined && { deptId: Number(params.deptId) }),
  };

  // 直接传递参数，不要嵌套在 params 对象中
  return requestClient.post('/auth/user/list', processedParams);
}

export async function saveUser(data: Record<string, any>) {
  return requestClient.post<UserApi.user>('/auth/user/save', data);
}

export async function deleteUserById(id: string) {
  return requestClient.delete<UserApi.user>(`/auth/user/delete?id=${id}`);
}

export async function getUserInfoApi() {
  return requestClient.get<UserInfo>('/auth/user/info');
}

export async function getDeptTreeApi() {
  return requestClient.get<TreeNode[]>('/auth/dept/tree');
}

// 添加删除部门的接口
export async function deleteDeptApi(id: number) {
  const numericId = Number(id);
  return requestClient.delete(`/auth/dept/delete?id=${numericId}`);
}

// 添加部门实体接口
export interface DeptData {
  id?: number;
  pid: number;
  name: string;
  description: string;
}

// 添加保存部门的接口
export async function saveDeptApi(data: DeptData) {
  const processedData = {
    ...data,
    pid: Number(data.pid),
    ...(data.id !== undefined && { id: Number(data.id) }),
  };
  return requestClient.post('/auth/dept/save', processedData);
}

// 添加获取部门详情的接口
export async function getDeptDetailApi(id: number) {
  const numericId = Number(id);
  return requestClient.get<DeptData>(`/auth/dept/${numericId}`);
}

// 添加根据部门ID删除用户的接口
export async function deleteUsersByDeptId(deptId: number) {
  const numericDeptId = Number(deptId);
  return requestClient.delete(`/auth/user/deleteByDeptId?deptId=${numericDeptId}`);
}

// 获取指定用户的角色
export async function getUserRolesApi(userId: string) {
  return requestClient.get<string[]>(`/auth/user/${userId}/roles`);
}

/**
 * 解锁用户
 */
export async function unlockUserApi(id: string) {
  return requestClient.post(`/auth/user/${id}/unlock`);
}

/**
 * 锁定用户
 */
export async function lockUserApi(id: string) {
  return requestClient.post(`/auth/user/${id}/lock`);
}

/**
 * 移动用户到指定部门
 */
export async function moveUserApi(userId: string, deptId: number) {
  return requestClient.post('/auth/user/move', {
    userId,
    deptId: Number(deptId),
  });
}

/**
 * 重置用户密码
 */
export async function resetUserPasswordApi(id: string) {
  return requestClient.post(`/auth/user/${id}/reset-password`);
}

// 定义密码修改的请求实体
export interface PasswordChangeDTO {
  userId: string;
  oldPassword: string;
  newPassword: string;
}

// 修改API函数
export async function alterUserPasswordApi(data: PasswordChangeDTO) {
  return requestClient.post('/auth/user/alterPw', data);
}

// 修改用户转移API
export function transferUserApi(userId: string, targetDeptId: number) {
  const numericDeptId = Number(targetDeptId);
  return requestClient.post(
    `/auth/user/${userId}/transfer?deptId=${numericDeptId}`,
  );
}
